<?php

namespace App\Util;


class LiteErrorResponse
{

    // SYSTEM_ERROR 系统繁忙

    /** query
     * ORDER_NOT_EXIST 订单不存在
     * EXCEED_VALID_TIME_RANGE  订单超出可查询期限
     * INVALID_QUERY_CHANNEL    非该设备该渠道发起的订单
     * NO_PERMISSION  无权限查询
     */


    /** 设备绑定
     * REPEAT_BIND   该设备已绑定商户、店铺
     *INVALID_PARAMETER 参数有误参数有误
     * BIND_FAIL  绑定失败
     * INVALID_MINI_APPID 应用创建者pid与设备所有者pid不一致
     * INVALID_SHOP_ID shop_id与商户pid不匹配
     * DEVICE_NOT_LEGAL  不支持当前设备调用绑定
     */

    /** 设备解绑
     * NOT_BIND 设备还未绑定
     * BIND_INFO_UNMATCH 与之前绑定信息不匹配
     * UNBIND_FAIL 解绑失败
     */

    /**支付
     * ORDER_HAS_CLOSED  订单已关闭，不允许再次支付
     * BIZ_ERROR 业务异常
     * ORDER_NOT_EXIST 订单不存在
     * EXCEED_VALID_TIME_RANGE 订单超出可查询期限
     * NO_PERMISSION  无权限查询
     * INVALID_QUERY_CHANNEL 非该设备该渠道发起的订单
     * ORDER_NOT_PAID
     * NO_PERMISSION
     * NOT_ALLOW_PARTIAL_REFUND
     * RECORD_NOT_EXIST
     */

    public $sub_code;
    public $sub_msg;

    public $error_code;
    public $error_msg;

    public function __construct($sub_code = "SYSTEM_ERROR", $sub_msg = "系统繁忙", $error_code = -1, $error_msg = "")
    {
        $this->sub_code = $sub_code;
        $this->sub_msg = $sub_msg;
        $this->error_code = $error_code;
        $this->error_msg = $error_msg;
    }

    public function __toString(): string
    {
        return json_encode(["sub_code" => $this->sub_code, "sub_msg" => $this->sub_msg, "error_code" => $this->error_code, "error_msg" => $this->error_msg]);
    }

    public function toObject(string $str): LiteErrorResponse
    {
        $error = json_decode($str, true);
        $this->sub_code = $error["sub_code"];
        $this->sub_msg = $error["sub_msg"];
        $this->error_code = $error["error_code"];
        $this->error_msg = $error["error_msg"];
        return $this;
    }

}